package com.lbs.jiepiaisi.service;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.IBinder;

import com.allenliu.versionchecklib.core.AVersionService;
import com.google.gson.Gson;
import com.lbs.jiepiaisi.bean.VersionBean;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.utils.Constant;


/**
 * Created by songdehua on 2017/10/30.
 */

public class DownloadService extends AVersionService {
    private Gson gson;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public void onResponses(AVersionService service, String response) {
        try {
            gson = new Gson();
            VersionBean downLoadBean = gson.fromJson(response, VersionBean.class);
            // 获取packagemanager的实例
            PackageManager packageManager = getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = null;
            try {
                packInfo = packageManager.getPackageInfo(getPackageName(),0);
                if(Integer.valueOf(downLoadBean.result.get(0).verNo) > packInfo.versionCode){
                    //showVersionDialog(downLoadBean.result.get(0).url,"检测到新版本",downLoadBean.result.get(0).updateDesc);
                    showVersionDialog(downLoadBean.result.get(0).url,"检测到新版本",downLoadBean.result.get(0).updateDesc);
                }else {
                    App.editor.putInt(Constant.CHECK_VERSION, 1);
                    App.editor.commit();
                }

            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }catch (Exception e){

        }
    }
}
